<?php
/**
 * 提现管理控制器
 * 作者: 王勇程 (laughskydragon@qq.com)
 */

namespace app\admin\controller;

class Withdraw extends AdminBase
{
    /**
     * 提现申请列表
     */
    public function withdrawList()
    {
        IS_POST && $this->jump($this->logicWithdraw->withdrawManage($this->param));
        
        $where = $this->getWhere();
        
        $this->assign('list', $this->logicWithdraw->getWithdrawList($where, true, 'id desc', DB_LIST_ROWS));
        $this->assign('stats', $this->logicWithdraw->getWithdrawStatistics());
        
        return $this->fetch('withdraw_list');
    }
    
    /**
     * 提现申请详情
     */
    public function withdrawDetail()
    {
        $id = $this->param['id'] ?? 0;
        
        if (!$id) {
            $this->jump([RESULT_ERROR, '参数错误']);
        }
        
        $info = $this->logicWithdraw->getWithdrawInfo(['id' => $id]);
        
        if (!$info) {
            $this->jump([RESULT_ERROR, '提现申请不存在']);
        }
        
        $this->assign('info', $info);
        
        return $this->fetch('withdraw_detail');
    }
    
    /**
     * 审核提现申请
     */
    public function withdrawAudit()
    {
        $withdrawId = $this->param['withdraw_id'] ?? 0;
        $withdrawStatus = $this->param['withdraw_status'] ?? 0;
        $adminRemark = $this->param['admin_remark'] ?? '';
        
        $data = [
            'withdraw_id' => $withdrawId,
            'withdraw_status' => $withdrawStatus,
            'admin_id' => MEMBER_ID,
            'admin_remark' => $adminRemark
        ];
        
        $this->jump($this->logicWithdraw->auditWithdraw($data));
    }
    
    /**
     * 确认打款
     */
    public function confirmPay()
    {
        $withdrawId = $this->param['withdraw_id'] ?? 0;
        $adminRemark = $this->param['admin_remark'] ?? '';
        
        $data = [
            'withdraw_id' => $withdrawId,
            'admin_id' => MEMBER_ID,
            'admin_remark' => $adminRemark
        ];
        
        $this->jump($this->logicWithdraw->confirmPay($data));
    }
    
    /**
     * 提现配置
     */
    public function withdrawConfig()
    {
        IS_POST && $this->jump($this->logicWithdraw->saveWithdrawConfig($this->param));
        
        $config = $this->logicWithdraw->getWithdrawConfig();
        
        $this->assign('config', $config);
        
        return $this->fetch('withdraw_config');
    }
    
    /**
     * 提现统计
     */
    public function withdrawStats()
    {
        $startTime = $this->param['start_time'] ?? '';
        $endTime = $this->param['end_time'] ?? '';
        
        $data = [
            'start_time' => $startTime,
            'end_time' => $endTime
        ];
        
        $stats = $this->logicWithdraw->getWithdrawStatistics($data);
        
        $this->assign('stats', $stats);
        $this->assign('start_time', $startTime);
        $this->assign('end_time', $endTime);
        
        return $this->fetch('withdraw_stats');
    }
    
    /**
     * 导出提现数据
     */
    public function withdrawExport()
    {
        $where = $this->getWhere();
        
        $this->logicWithdraw->exportWithdrawList($where);
    }
    
    /**
     * 获取查询条件
     */
    private function getWhere()
    {
        return $this->logicWithdraw->getWhere($this->param);
    }
}